package com.dh.edu.interceptor;

import com.dh.edu.rule.RibbonParaContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class UserIdInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeaders("userId").toString();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        RibbonParaContext.set(map);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        RibbonParaContext.clear();
    }
}
